package com.jse.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.concurrent.ThreadLocalRandom;

import javax.imageio.ImageIO;

public class Imgs {

	static {
		ImageIO.setUseCache(false);
	}
	
	public static String base64(Object obj,String type) throws Exception {
		if(obj instanceof InputStream in) {
			try(in){
				BufferedImage img= ImageIO.read(in);
				ByteArrayOutputStream out=new ByteArrayOutputStream();
				ImageIO.write(img, type,ImageIO.createImageOutputStream(out));
				return Base64.getEncoder().encodeToString(out.toByteArray()).replace(" ", "+");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		throw new RuntimeException("obj:{} is not support case to type:{} base64!");
	}
	
	public static void base64Img() throws FileNotFoundException, IOException {
		byte[] bs=Base64.getDecoder().decode(
	new FileInputStream("D:\\tomcat\\webapps\\upload\\cps\\20220601\\352406451953598464.png")
	.readAllBytes());
		new FileOutputStream("D:\\tomcat\\webapps\\upload\\cps\\20220601\\1.png")
		.write(bs);
		
	}
	
	/**
	 * 随机干扰圈
	 * @param g
	 * @param width
	 * @param height
	 * @param count 干扰数量
	 * @param type circle/圆圈 shear/扭曲干扰 null/默认
	 */
	public static void drawInterfere(Graphics2D g,int width,int height,int count,String type) {
		final ThreadLocalRandom random = ThreadLocalRandom.current();
		if(type==null)type="";
		for (int i = 0; i < count; i++) {
			if(type.equals("circle")) {//圆圈
				g.setColor(Lang.randomColor());
				g.drawOval(random.nextInt(width), random.nextInt(height), random.nextInt(height >> 1), random.nextInt(height >> 1));
			}else if(type.equals("shear")){//扭曲干扰
				if(type.equals("shear")) {//x
					int period = random.nextInt(width);
					boolean borderGap = true;
					int frames = 1;
					int phase = random.nextInt(2);
					for (int j = 0; j < height; j++) {
						double d = (double) (period >> 1) * Math.sin((double) j / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
						g.copyArea(0, j, width, 1, (int) d, 0);
						if (borderGap) {
							g.setColor(Color.WHITE);
							g.drawLine((int) d, j, 0, j);
							g.drawLine((int) d + width, j, width, j);
						}
					}
				}
				if(type.equals("shear")) {//y
					int period = random.nextInt(height >> 1);
					int frames = 20;
					int phase = 7;
					for (int j = 0; j < width; j++) {
						double d = (double) (period >> 1) * Math.sin((double) j / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
						g.copyArea(j, 0, 1, height, 0, (int) d);
						g.setColor(Color.WHITE);
						// 擦除原位置的痕迹
						g.drawLine(j, (int) d, j, 0);
						g.drawLine(j, (int) d + height, j, height);
					}
				}
				// The thick line is in fact a filled polygon
				g.setColor(Lang.randomColor());
				int x1=0;
				int x2=random.nextInt(height) + 1;
				int y1=width;
				int y2=random.nextInt(height) + 1;
				int dX = x2 - x1;
				int dY = y2 - y1;
				// line length
				double lineLength = Math.sqrt(dX * dX + dY * dY);
				double scale = (double) (count) / (2 * lineLength);
				// The x and y increments from an endpoint needed to create a!rectangle...
				double ddx = -scale * (double) dY;
				double ddy = scale * (double) dX;
				ddx += (ddx > 0) ? 0.5 : -0.5;
				ddy += (ddy > 0) ? 0.5 : -0.5;
				int dx = (int) ddx;
				int dy = (int) ddy;
				// Now we can compute the corner points...
				int[] xPoints = new int[4];
				int[] yPoints = new int[4];
				xPoints[0] = x1 + dx;
				yPoints[0] = y1 + dy;
				xPoints[1] = x1 - dx;
				yPoints[1] = y1 - dy;
				xPoints[2] = x2 - dx;
				yPoints[2] = y2 - dy;
				xPoints[3] = x2 + dx;
				yPoints[3] = y2 + dy;
				g.fillPolygon(xPoints, yPoints, 4);
			}else {
				//默认干扰为线
				int xs = random.nextInt(width);
				int ys = random.nextInt(height);
				int xe = xs + random.nextInt(width / 8);
				int ye = ys + random.nextInt(height / 8);
				g.setColor(Lang.randomColor(140, 255));
				g.drawLine(xs, ys, xe, ye);
			}
		}
	}
	
	public static void main(String[] args) {
		try {
//			System.out.println(base64(new FileInputStream("D:/dl.png"), "jpg"));
			base64Img();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
